package com.ohuang.apkpatch

import java.io.*
import java.math.BigInteger
import java.security.MessageDigest


fun compareFile(firFile: String, secFile: String): Boolean {
    val oldFile = File(firFile)
    val newFile = File(secFile)
    try {
        val oldFileMd5 = getFileMD5(oldFile)
        val newFileMd5 = getFileMD5(newFile)
        if (oldFileMd5 != null && newFileMd5 != null) {
            if (oldFileMd5 == newFileMd5) {
                return true
            }
        }
    } catch (_: Throwable) {

    }
    return false
}


fun isFileUpdate(oldName: String?, newName: String?): Boolean {
    val oldFile = File(oldName)
    val newFile = File(newName)
    val oldFileMd5 = getFileMD5(oldFile)
    val newFileMd5 = getFileMD5(newFile)
    if (oldFileMd5 == null) {
        return true
    }
    return if (newFileMd5 == null) {
        false
    } else oldFileMd5 != newFileMd5
}

private fun getFileMD5(file: File): String? {
    if (!file.isFile) {
        return null
    }
    var digest: MessageDigest? = null
    var inStream: FileInputStream? = null
    val buffer = ByteArray(1024)
    var len: Int
    try {
        digest = MessageDigest.getInstance("MD5")
        inStream = FileInputStream(file)
        while (inStream.read(buffer, 0, 1024).also { len = it } != -1) {
            digest.update(buffer, 0, len)
        }
        inStream.close()
    } catch (e: Exception) {
        return null
    }
    val bigInt = BigInteger(1, digest.digest())
    return bigInt.toString(16)
}


/**
 * 比较文件夹内容，复制已修改的文件到新目录
 */
fun copyDifferenceFile(oldFile: String, newFile: String, outFile: String) {
    copyDifferenceFile(File(oldFile), File(newFile), File(outFile))
}

fun copyDifferenceFile(oldFile: File, newFile: File, outFile: File) {

    if (newFile.exists()) {

        if (newFile.isFile) {
            val compareFile = compareFile(oldFile.absolutePath, newFile.absolutePath)
            if (!compareFile) {
                copyFile(newFile.absolutePath, outFile.absolutePath, true)
            }
        } else {
            newFile.listFiles()?.forEach {
                val outAbsolutePath = outFile.absolutePath + "/${it.name}"
                val oldAbsolutePath = oldFile.absolutePath + "/${it.name}"
                if (it.isFile) {
                    val compareFile = compareFile(oldAbsolutePath, it.absolutePath)
                    if (!compareFile) {
                        copyFile(it.absolutePath, outAbsolutePath, true)
                    }
                } else {
                    copyDifferenceFile(File(oldAbsolutePath), it, File(outAbsolutePath))
                }
            }
        }
    }
}

fun readText(file: File): String {
    var fileInputStream = FileInputStream(file)
    return fileInputStream.use {
        var readBytes = it.readBytes()
        String(readBytes)
    }

}

fun writeText(file: File, text: String) {
    if (!file.parentFile.exists()) {
        file.parentFile.mkdirs()
    }
    var fileOutputStream = FileOutputStream(file)
    fileOutputStream.use {
        var encodeToByteArray = text.encodeToByteArray()
        it.write(encodeToByteArray)
    }

}